package cn.edu.scau.cmi.tangqingning.controll;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.scau.cmi.tangqingning.dao.UserDao;
import cn.edu.scau.cmi.tangqingning.dao.impl.UserDaoImpl;
import cn.edu.scau.cmi.tangqingning.model.Student;

/**
 * 控制层-更新学生信息的逻辑处理<br>
 * 当客户使用get方式请求Servlet时，Web容器调用doGet方法处理请求，通过调用UserDaoImpl的updateStudent方法更新学生的个人信息
 * @author 刘岳鑫
 *@see cn.edu.scau.cmi.tangqingning.dao.impl.UserDaoImpl#updateStudent
 */
public class UpdateStudentServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException 如果一个错误发生，则抛出此异常
	 * @throws IOException 如果一个错误发生，则抛出此异常
	 */
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * 重写doGet方法
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException 如果一个错误发生，则抛出此异常
	 * @throws IOException 如果一个错误发生，则抛出此异常
	 */

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		long stId = Long.parseLong(request.getParameter("stId"));
		System.out.print(request.getParameter("stId"));
		String stName = request.getParameter("stName");
		String stSex = request.getParameter("stSex");
		String stAge = request.getParameter("stAge");
		long stTel = Long.parseLong(request.getParameter("stTel"));
		String stDept = request.getParameter("stDept");
		String stAddress = request.getParameter("stAddress");
		String stGroup = request.getParameter("stGroup");
		Student stu = new Student(stId, stName, stSex, stAge, stTel, stDept, stAddress, stGroup);
		UserDao student = new UserDaoImpl();

		boolean flag = student.updateStudent(stu);
		if (flag) {
			request.setAttribute("msg", "<------修改成功------>");
			request.getRequestDispatcher("userMain.jsp").forward(request, response);
		} else {
			request.setAttribute("msg", "<------修改失败------>");
			request.getRequestDispatcher("userMain.jsp").forward(request, response);
		}
	}

}
